﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.swinchester.windowsevents.core;
using System.IO;
using System.Diagnostics.Eventing.Reader;

namespace org.swinchester.windowsevents.mongodb
{
    public class WindowsEventsToMongoDB
    {
        public MongoDbConfig dbConfig { get; set; }

        public EventSubscriptionConfig SubscriberConfig { get; set; }
        public EventSubscriptionConfig CatchupConfig { get; set; }
        
        private MongoWindowsEventEntityDao eventDao;
        private MongoMachineTrackingEntityDao computerDao;

        public MongoWindowsEventEntityDao MongoEventDao()
        {
            if (eventDao == null)
            {
                eventDao = new MongoWindowsEventEntityDao { MongoDbConfig = dbConfig };
            }
            return eventDao;
        }

        public MongoMachineTrackingEntityDao ComputerDao()
        {
            if (computerDao == null)
            {
                computerDao = new MongoMachineTrackingEntityDao { MongoDbConfig = dbConfig };
            }
            return computerDao;
        }

        private void CatchupEvents()
        {
            if (CatchupConfig != null)
            {
                long lLastEvent = ComputerDao().GetLastEventRecordId(Environment.MachineName);
                CatchupConfig.EventLogQuery = CatchupConfig.EventLogQuery.Replace("paramEVENTRECORDID", lLastEvent.ToString());
                EventQueryReader eqr = new EventQueryReader();
                eqr.SubscriptionConfig = CatchupConfig;
                eqr.EventQueryRecordRead += new EventReaderBase.GenericEventRecordHandler(eqr_EventQueryRecordRead);
                eqr.Exhaust();
            }
        }

        void eqr_EventQueryRecordRead(object sender, EventArgs args)
        {
            EventRecord evtRecord = (EventRecord)sender;
            MongoEventDao().Insert(evtRecord);
            ComputerDao().UpdateEventRecordId(evtRecord.MachineName, long.Parse(evtRecord.RecordId.ToString())); 
        }


        public void SubscribeToEvents()
        {
            CatchupEvents();
            EventSubscriber es = new EventSubscriber();
            es.SubscriptionConfig = SubscriberConfig;
            es.EventRecordRead += new EventReaderBase.GenericEventRecordHandler(eqr_EventRecordRead);
            es.Register();

            while (true)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }

        void eqr_EventRecordRead(object sender, EventArgs args)
        {
            EventRecord evtRecord = (EventRecord)sender;
            MongoEventDao().Insert(evtRecord);
            ComputerDao().UpdateEventRecordId(evtRecord.MachineName, long.Parse(evtRecord.RecordId.ToString()));       
        }

        public static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("EventLogQuery.txt");
            string szQuery = sr.ReadToEnd();

            MongoDbConfig dbConfig = new MongoDbConfig { ConnectionString = "mongodb://localhost/?safe=true", Database = "test", ComputerEntities = "machines", EventEntities = "events" };
            EventSubscriptionConfig subConfig = new EventSubscriptionConfig { EventLogName="Application", EventPathType = System.Diagnostics.Eventing.Reader.PathType.LogName, EventLogQuery = szQuery};

            WindowsEventsToMongoDB winThing = new WindowsEventsToMongoDB { dbConfig = dbConfig, SubscriberConfig = subConfig };
            winThing.SubscribeToEvents();

        }

    }
}
